<?php

namespace common\model;

use think\model\RelationModel;
use think\module\Module;

class NoteModel extends RelationModel
{
    const DEMAND_TYPE_DEFAULT = 'default';
    const DEMAND_TYPE_FEEDBACK = 'feedback';
    const DEMAND_TYPE_REVIEW_FEEDBACK = 'review_feedback';
    const DEMAND_TYPE_REPAIR_FEEDBACK = 'repair_feedback';
    const DEMAND_TYPE_DEMAND = 'demand';
    const DEMAND_TYPE_COMMENT = 'comment';

    const DEMAND_STATUS_DEFAULT = 'default'; // 未接受 默认状态
    const DEMAND_STATUS_PENDING = 'pending'; // 待发起返修
    const DEMAND_STATUS_PROCESSING = 'processing'; // 进行中
    const DEMAND_STATUS_FINAL = 'final'; // 已完成
    const DEMAND_STATUS_REJECTED = 'rejected'; // 拒绝

    // 审核反馈 已经接受过的状态集合
    const NOTE_DEMAND_STATUS_ACCEPTED = [ NoteModel::DEMAND_STATUS_PROCESSING, NoteModel::DEMAND_STATUS_FINAL];

    const NOT_REPAIRED_DEMAND_STATUS = [NoteModel::DEMAND_STATUS_DEFAULT, NoteModel::DEMAND_STATUS_PENDING, NoteModel::DEMAND_STATUS_REJECTED];
    const REPAIRED_DEMAND_STATUS = [NoteModel::DEMAND_STATUS_PROCESSING, NoteModel::DEMAND_STATUS_FINAL];


    const REASON_TYPE_DEMAND_CHANGE = '需求变更';
    const REASON_TYPE_PROJECT_QUALITY = '项目质量';
    const REASON_TYPE_BASIC_QUALITY = '基础质量';
    const REASON_TYPE_ADD = '新增';
    const REASON_TYPE_DEPRECATE = '弃用';
    const REASON_TYPE_ENABLE = '启用';

    const REASON_TYPE_TOTAL = [
        self::REASON_TYPE_DEMAND_CHANGE,
        self::REASON_TYPE_PROJECT_QUALITY,
        self::REASON_TYPE_BASIC_QUALITY,
        self::REASON_TYPE_ADD,
        self::REASON_TYPE_DEPRECATE,
        self::REASON_TYPE_ENABLE,
    ];

    // 需要返修的需求类型
    const REASON_TYPE_REPAIR = [
        self::REASON_TYPE_DEMAND_CHANGE,
        self::REASON_TYPE_PROJECT_QUALITY,
        self::REASON_TYPE_BASIC_QUALITY,

    ];

    //自动验证
    protected $_validate = [
        ['type', ['text', 'audio', 'video'], '', self::EXISTS_VALIDATE, 'in'],
        ['link_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['stick', ['yes', 'no'], '', self::EXISTS_VALIDATE, 'in'],
        ['parent_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['module_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['json', '', '', self::EXISTS_VALIDATE, 'array'],
        ['review_param', '', '', self::EXISTS_VALIDATE, 'array'],
        ['demand_type', ['default', 'feedback', 'review_feedback', 'repair_feedback', 'demand', 'comment', 'task_progress'], '', self::EXISTS_VALIDATE, 'in'],
        ['module_code', ['asset', 'shot', 'todo'], '', self::EXISTS_VALIDATE, 'in'],
    ];

    //自动完成
    protected $_auto = [
        ['tenant_id', 'auto_fill_tenant_id', self::MODEL_INSERT, 'function'],
        ['json', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['review_param', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['last_updated_by', 'fill_created_by', self::MODEL_BOTH, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['last_updated', 'time', self::MODEL_BOTH, 'function'],
        ['uuid', 'create_uuid', self::MODEL_INSERT, 'function'],
        ['text', 'fill_text_default_val', self::MODEL_INSERT, 'function'],
        ['review_param', 'json_encode', self::EXISTS_VALIDATE, 'function'],
    ];

    // 隐藏字段
    protected $hidden = ['json'];

    /**
     * 获取器：自定义字段
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getJsonAttr($value, $data)
    {
        // 当前模块的自定义字段配置
        $currentModuleCode = \request()->getModuleCode();
        $fieldDict = Module::$moduleDictData['field_index_by_code']['note']['custom'];

        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        // 把自定义值写入追加字段
        foreach ($fieldDict as $filed => $config) {
            if (array_key_exists($filed, $jsonDictData)) {
                $this->appendCustomField[$filed] = $jsonDictData[$filed];
            } else {
                // 没有值赋空值
                $this->appendCustomField[$filed] = "";
            }
        }

        return $jsonDictData;
    }

    /**
     * 获取器：最后更新时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getLastUpdatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：创建时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCreatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器: 审核额外参数
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getReviewParamAttr($value, $data)
    {
        $jsonDictData = [];
        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        }
        return $jsonDictData;
    }
}
